import Axios, { type AxiosInstance, type AxiosRequestConfig } from "axios";
import Cookies from "js-cookie";
import crypto from "@/utils/crypto";
import { useProStore } from "../store/pro";
import type {
  PureHttpError,
  PureHttpRequestConfig,
  PureHttpResponse,
  RequestMethods,
  result
} from "./types.d";
// const servire = axios.create({
//   baseURL: "http://localhost:3000",
//   timeout: 1000,
//   headers: {
//     "Content-Type": "application/json"
//   }
// })

const iAppSecret = "INHYV67NLAG10JAG76CU";
const iAppId = "IFAPOUIBIUY";

const defaultConfig: AxiosRequestConfig = {
  // 请求超时时间
  baseURL: "/api",
  timeout: 3000,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  }
};

class PureHttp {
  constructor() {
    this.httpInterceptorsRequest();
    this.httpInterceptorsResponse();
  }

  private static AxiosInstance: AxiosInstance = Axios.create(defaultConfig);

  private httpInterceptorsRequest() {
    PureHttp.AxiosInstance.interceptors.request.use(
      async (config: PureHttpRequestConfig): Promise<any> => {
        const timeStamp = new Date().getTime();

        // const commonVar = inject(commonVarKey);
        const store = useProStore();

        const ParamU = {
          UniSess: Cookies.get("UniSess"),
          SessLib: Cookies.get("SessLib") || "xhcom",
          SessPrd: store.pro,
          SessFun: "web"
        };
        const paramA = JSON.stringify(config.a);
        const sign =
          iAppSecret +
          timeStamp +
          paramA +
          iAppId +
          JSON.stringify(ParamU) +
          timeStamp +
          iAppSecret;

        config.params = {
          timestamp: timeStamp,
          appid: iAppId,
          sign: crypto.Sign(sign),
          y: "01",
          ec: "2"
        };

        config.data = {
          a: crypto.base64Encrypt(paramA),
          u: crypto.base64Encrypt(JSON.stringify(ParamU))
        };

        // return config;
        return new Promise((resolve) => {
          resolve(config);
        });
      },
      (error) => {
        return Promise.reject(error);
      }
    );
  }

  private httpInterceptorsResponse() {
    PureHttp.AxiosInstance.interceptors.response.use(
      async (response: PureHttpResponse): Promise<any> => {
        // console.log(response);
        if (response.status === 200) {
          return response.data;
        }
        // return response;
      },
      (error: PureHttpError) => {
        return Promise.reject(error);
      }
    );
  }

  /** 通用请求工具函数 */
  public request(
    method: RequestMethods,
    url: string,
    param?: AxiosRequestConfig,
    axiosConfig?: PureHttpRequestConfig
  ): Promise<result> {
    const config = {
      method,
      url,
      ...param,
      ...axiosConfig
    } as PureHttpRequestConfig;

    // 单独处理自定义请求/响应回调
    return new Promise((resolve, reject) => {
      PureHttp.AxiosInstance.request(config)
        .then((response: any) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
}

export const http = new PureHttp();
